home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gximag3x.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  28.5 KB  |  861 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gximag3x.c,v 1.7 2000/09/19 19:00:37 lpd Exp $ */
  20. /* ImageType 3x image implementation */
  21. /****** THE REAL WORK IS NYI ******/
  22. #include "math_.h"        /* for ceil, floor */
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gsbitops.h"
  27. #include "gscspace.h"
  28. #include "gscpixel.h"
  29. #include "gsstruct.h"
  30. #include "gxdevice.h"
  31. #include "gxdevmem.h"
  32. #include "gximag3x.h"
  33. #include "gxistate.h"
  34. #include "gdevbbox.h"
  35.  
  36. extern_st(st_color_space);
  37.  
  38. /* Forward references */
  39. private dev_proc_begin_typed_image(gx_begin_image3x);
  40. private image_enum_proc_plane_data(gx_image3x_plane_data);
  41. private image_enum_proc_end_image(gx_image3x_end_image);
  42. private image_enum_proc_flush(gx_image3x_flush);
  43. private image_enum_proc_planes_wanted(gx_image3x_planes_wanted);
  44.  
  45. /* GC descriptor */
  46. private_st_gs_image3x();
  47.  
  48. /* Define the image type for ImageType 3x images. */
  49. const gx_image_type_t gs_image_type_3x = {
  50.     &st_gs_image3x, gx_begin_image3x, gx_data_image_source_size,
  51.     gx_image_no_sput, gx_image_no_sget, gx_image_default_release,
  52.     IMAGE3X_IMAGETYPE
  53. };
  54. private const gx_image_enum_procs_t image3x_enum_procs = {
  55.     gx_image3x_plane_data, gx_image3x_end_image,
  56.     gx_image3x_flush, gx_image3x_planes_wanted
  57. };
  58.  
  59. /* Initialize an ImageType 3x image. */
  60. private void
  61. gs_image3x_mask_init(gs_image3x_mask_t *pimm)
  62. {
  63.     pimm->InterleaveType = 0;    /* not a valid type */
  64.     pimm->has_Matte = false;
  65.     gs_data_image_t_init(&pimm->MaskDict, 1);
  66.     pimm->MaskDict.BitsPerComponent = 0;    /* not supplied */
  67. }
  68. void
  69. gs_image3x_t_init(gs_image3x_t * pim, const gs_color_space * color_space)
  70. {
  71.     gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space);
  72.     pim->type = &gs_image_type_3x;
  73.     gs_image3x_mask_init(&pim->Opacity);
  74.     gs_image3x_mask_init(&pim->Shape);
  75. }
  76.  
  77. /*
  78.  * We implement ImageType 3 images by interposing a mask clipper in
  79.  * front of an ordinary ImageType 1 image.  Note that we build up the
  80.  * mask row-by-row as we are processing the image.
  81.  *
  82.  * We export a generalized form of the begin_image procedure for use by
  83.  * the PDF and PostScript writers.
  84.  */
  85.  
  86. typedef struct image3x_channel_state_s {
  87.     gx_image_enum_common_t *info;
  88.     gx_device *mdev;        /* gx_device_memory in default impl. */
  89.                 /* (only for masks) */
  90.     gs_image3_interleave_type_t InterleaveType;
  91.     int width, height, full_height, depth;
  92.     byte *data;            /* (if chunky) */
  93.     /* Only the following change dynamically. */
  94.     int y;
  95.     int skip;            /* only for masks, # of rows to skip, */
  96.                 /* see below */
  97. } image3x_channel_state_t;
  98. typedef struct gx_image3x_enum_s {
  99.     gx_image_enum_common;
  100.     gx_device *pcdev;        /* gx_device_mask_clip in default impl. */
  101.     int num_components;        /* (not counting masks) */
  102.     int bpc;            /* pixel BitsPerComponent */
  103.     gs_memory_t *memory;
  104. #define NUM_MASKS 2        /* opacity, shape */
  105.     image3x_channel_state_t mask[NUM_MASKS], pixel;
  106. } gx_image3x_enum_t;
  107.  
  108. extern_st(st_gx_image_enum_common);
  109. gs_private_st_suffix_add9(st_image3x_enum, gx_image3x_enum_t,
  110.   "gx_image3x_enum_t", image3x_enum_enum_ptrs, image3x_enum_reloc_ptrs,
  111.   st_gx_image_enum_common, pcdev, mask[0].info, mask[0].mdev, mask[0].data,
  112.   mask[1].info, mask[1].mdev, mask[1].data, pixel.info, pixel.data);
  113.  
  114. /*
  115.  * Begin a generic ImageType 3x image, with client handling the creation of
  116.  * the mask image and mask clip devices.
  117.  */
  118. typedef struct image3x_channel_values_s {
  119.     gs_matrix matrix;
  120.     gs_point corner;
  121.     gs_int_rect rect;
  122.     gs_image_t image;
  123. } image3x_channel_values_t;
  124. private bool is_multiple(P2(int x, int y));
  125. private int check_image3x_mask(P6(const gs_image3x_t *pim,
  126.                   const gs_image3x_mask_t *pimm,
  127.                   const image3x_channel_values_t *ppcv,
  128.                   image3x_channel_values_t *pmcv,
  129.                   image3x_channel_state_t *pmcs,
  130.                   gs_memory_t *mem));
  131. int
  132. gx_begin_image3x_generic(gx_device * dev,
  133.             const gs_imager_state *pis, const gs_matrix *pmat,
  134.             const gs_image_common_t *pic, const gs_int_rect *prect,
  135.             const gx_drawing_color *pdcolor,
  136.             const gx_clip_path *pcpath, gs_memory_t *mem,
  137.             image3x_make_mid_proc_t make_mid,
  138.             image3x_make_mcde_proc_t make_mcde,
  139.             gx_image_enum_common_t **pinfo)
  140. {
  141.     const gs_image3x_t *pim = (const gs_image3x_t *)pic;
  142.     gx_image3x_enum_t *penum;
  143.     gx_device *pcdev = 0;
  144.     image3x_channel_values_t mask[2], pixel;
  145.     gs_matrix mat;
  146.     gx_device *midev[2];
  147.     gx_image_enum_common_t *minfo[2];
  148.     gs_int_point origin[2];
  149.     int code;
  150.     int i;
  151.  
  152.     /* Validate the parameters. */
  153.     if (pim->Height <= 0)
  154.     return_error(gs_error_rangecheck);
  155.     penum = gs_alloc_struct(mem, gx_image3x_enum_t, &st_image3x_enum,
  156.                 "gx_begin_image3x");
  157.     if (penum == 0)
  158.     return_error(gs_error_VMerror);
  159.     /* Initialize pointers now in case we bail out. */
  160.     penum->mask[0].info = 0, penum->mask[0].mdev = 0, penum->mask[0].data = 0;
  161.     penum->mask[1].info = 0, penum->mask[1].mdev = 0, penum->mask[1].data = 0;
  162.     penum->pixel.info = 0, penum->pixel.data = 0;
  163.     if (prect)
  164.     pixel.rect = *prect;
  165.     else {
  166.     pixel.rect.p.x = pixel.rect.p.y = 0;
  167.     pixel.rect.q.x = pim->Width;
  168.     pixel.rect.q.y = pim->Height;
  169.     }
  170.     if ((code = gs_matrix_invert(&pim->ImageMatrix, &pixel.matrix)) < 0 ||
  171.     (code = gs_point_transform(pim->Width, pim->Height, &pixel.matrix,
  172.                    &pixel.corner)) < 0 ||
  173.     (code = check_image3x_mask(pim, &pim->Opacity, &pixel, &mask[0],
  174.                    &penum->mask[0], mem)) < 0 ||
  175.     (code = check_image3x_mask(pim, &pim->Shape, &pixel, &mask[1],
  176.                    &penum->mask[1], mem)) < 0
  177.     ) {
  178.     goto out0;
  179.     }
  180.     penum->num_components =
  181.     gs_color_space_num_components(pim->ColorSpace);
  182.     gx_image_enum_common_init((gx_image_enum_common_t *) penum,
  183.                   (const gs_data_image_t *)pim,
  184.                   &image3x_enum_procs, dev,
  185.                   1 + penum->num_components,
  186.                   pim->format);
  187.     penum->pixel.width = pixel.rect.q.x - pixel.rect.p.x;
  188.     penum->pixel.height = pixel.rect.q.y - pixel.rect.p.y;
  189.     penum->pixel.full_height = pim->Height;
  190.     penum->pixel.y = 0;
  191.     if (penum->mask[0].data || penum->mask[1].data) {
  192.     /* Also allocate a row buffer for the pixel data. */
  193.     penum->pixel.data =
  194.         gs_alloc_bytes(mem,
  195.                (penum->pixel.width * pim->BitsPerComponent *
  196.                 penum->num_components + 7) >> 3,
  197.                "gx_begin_image3x(pixel.data)");
  198.     if (penum->pixel.data == 0) {
  199.         code = gs_note_error(gs_error_VMerror);
  200.         goto out1;
  201.     }
  202.     }
  203.     penum->bpc = pim->BitsPerComponent;
  204.     penum->memory = mem;
  205.     if (pmat == 0)
  206.     pmat = &ctm_only(pis);
  207.     for (i = 0; i < NUM_MASKS; ++i) {
  208.     gs_rect mrect;
  209.     gx_device *mdev;
  210.     /*
  211.      * The mask data has to be defined in a DevicePixel color space
  212.      * of the correct depth so that no color mapping will occur.
  213.      */
  214.     /****** FREE COLOR SPACE ON ERROR OR AT END ******/
  215.     gs_color_space *pmcs;
  216.  
  217.     if (penum->mask[i].depth == 0) {    /* mask not supplied */
  218.         midev[0] = 0;
  219.         minfo[0] = 0;
  220.         continue;
  221.     }
  222.     pmcs =  gs_alloc_struct(mem, gs_color_space, &st_color_space,
  223.                 "gx_begin_image3x_generic");
  224.     if (pmcs == 0)
  225.         return_error(gs_error_VMerror);
  226.     gs_cspace_init_DevicePixel(pmcs, penum->mask[i].depth);
  227.     mrect.p.x = mrect.p.y = 0;
  228.     mrect.q.x = penum->mask[i].width;
  229.     mrect.q.y = penum->mask[i].height;
  230.     if ((code = gs_matrix_multiply(&mask[i].matrix, pmat, &mat)) < 0 ||
  231.         (code = gs_bbox_transform(&mrect, &mat, &mrect)) < 0
  232.         )
  233.         return code;
  234.     origin[i].x = floor(mrect.p.x);
  235.     origin[i].y = floor(mrect.p.y);
  236.     code = make_mid(&mdev, dev,
  237.             (int)ceil(mrect.q.x) - origin[i].x,
  238.             (int)ceil(mrect.q.y) - origin[i].y,
  239.             penum->mask[i].depth, mem);
  240.     if (code < 0)
  241.         goto out1;
  242.     penum->mask[i].mdev = mdev;
  243.     gs_image_t_init_gray(&mask[i].image, pis); /* gray is bogus */
  244.     mask[i].image.ColorSpace = pmcs;
  245.     mask[i].image.adjust = false;
  246.     {
  247.         const gx_image_type_t *type1 = mask[i].image.type;
  248.         const gs_image3x_mask_t *pixm =
  249.         (i == 0 ? &pim->Opacity : &pim->Shape);
  250.  
  251.         *(gs_data_image_t *)&mask[i].image = pixm->MaskDict;
  252.         mask[i].image.type = type1;
  253.         mask[i].image.BitsPerComponent = pixm->MaskDict.BitsPerComponent;
  254.     }
  255.     {
  256.         gs_matrix m_mat;
  257.  
  258.         /*
  259.          * Adjust the translation for rendering the mask to include a
  260.          * negative translation by origin.{x,y} in device space.
  261.          */
  262.         m_mat = *pmat;
  263.         m_mat.tx -= origin[i].x;
  264.         m_mat.ty -= origin[i].y;
  265.         /*
  266.          * Note that pis = NULL here, since we don't want to have to
  267.          * create another imager state with default log_op, etc.
  268.          * dcolor = NULL is OK because this is an opaque image with
  269.          * CombineWithColor = false.
  270.          */
  271.         code = gx_device_begin_typed_image(mdev, NULL, &m_mat,
  272.                    (const gs_image_common_t *)&mask[i].image,
  273.                            &mask[i].rect, NULL, NULL,
  274.                            mem, &penum->mask[i].info);
  275.         if (code < 0)
  276.         goto out2;
  277.     }
  278.     midev[i] = mdev;
  279.     minfo[i] = penum->mask[i].info;
  280.     }
  281.     gs_image_t_init(&pixel.image, pim->ColorSpace);
  282.     {
  283.     const gx_image_type_t *type1 = pixel.image.type;
  284.  
  285.     *(gs_pixel_image_t *)&pixel.image = *(const gs_pixel_image_t *)pim;
  286.     pixel.image.type = type1;
  287.     }
  288.     code = make_mcde(dev, pis, pmat, (const gs_image_common_t *)&pixel.image,
  289.              prect, pdcolor, pcpath, mem, &penum->pixel.info,
  290.              &pcdev, midev, minfo, origin, pim);
  291.     if (code < 0)
  292.     goto out3;
  293.     penum->pcdev = pcdev;
  294.     /*
  295.      * Set num_planes, plane_widths, and plane_depths from the values in the
  296.      * enumerators for the mask(s) and the image data.
  297.      */
  298.     {
  299.     int added_depth = 0;
  300.     int pi = 0;
  301.  
  302.     for (i = 0; i < NUM_MASKS; ++i) {
  303.         if (penum->mask[i].depth == 0)    /* no mask */
  304.         continue;
  305.         switch (penum->mask[i].InterleaveType) {
  306.         case interleave_chunky:
  307.         /* Add the mask data to the depth of the image data. */
  308.         added_depth += pim->BitsPerComponent;
  309.         break;
  310.         case interleave_separate_source:
  311.         /* Insert the mask as a separate plane. */
  312.         penum->plane_widths[pi] = penum->mask[i].width;
  313.         penum->plane_depths[pi] = penum->mask[i].depth;
  314.         ++pi;
  315.         break;
  316.         default:        /* can't happen */
  317.         code = gs_note_error(gs_error_Fatal);
  318.         goto out3;
  319.         }
  320.     }
  321.     memcpy(&penum->plane_widths[pi], &penum->pixel.info->plane_widths[0],
  322.            penum->pixel.info->num_planes * sizeof(penum->plane_widths[0]));
  323.     memcpy(&penum->plane_depths[pi], &penum->pixel.info->plane_depths[0],
  324.            penum->pixel.info->num_planes * sizeof(penum->plane_depths[0]));
  325.     penum->plane_depths[pi] += added_depth;
  326.     penum->num_planes = pi + penum->pixel.info->num_planes;
  327.     }
  328.     if (midev[0])
  329.     gx_device_retain(midev[0], true); /* will free explicitly */
  330.     if (midev[1])
  331.     gx_device_retain(midev[1], true); /* ditto */
  332.     gx_device_retain(pcdev, true); /* ditto */
  333.     *pinfo = (gx_image_enum_common_t *) penum;
  334.     return 0;
  335.   out3:
  336.     if (penum->mask[1].info)
  337.     gx_image_end(penum->mask[1].info, false);
  338.     if (penum->mask[0].info)
  339.     gx_image_end(penum->mask[0].info, false);
  340.   out2:
  341.     if (penum->mask[1].mdev) {
  342.     gs_closedevice(penum->mask[1].mdev);
  343.     gs_free_object(mem, penum->mask[1].mdev,
  344.                "gx_begin_image3x(mask[1].mdev)");
  345.     }
  346.     if (penum->mask[0].mdev) {
  347.     gs_closedevice(penum->mask[0].mdev);
  348.     gs_free_object(mem, penum->mask[0].mdev,
  349.                "gx_begin_image3x(mask[0].mdev)");
  350.     }
  351.   out1:
  352.     gs_free_object(mem, penum->mask[0].data, "gx_begin_image3x(mask[0].data)");
  353.     gs_free_object(mem, penum->mask[1].data, "gx_begin_image3x(mask[1].data)");
  354.     gs_free_object(mem, penum->pixel.data, "gx_begin_image3x(pixel.data)");
  355.   out0:
  356.     gs_free_object(mem, penum, "gx_begin_image3x");
  357.     return code;
  358. }
  359. private bool
  360. is_multiple(int x, int y)
  361. {
  362.     return (x % y == 0 || y % x == 0);
  363. }
  364. private bool
  365. check_image3x_extent(floatp mask_coeff, floatp data_coeff)
  366. {
  367.     if (mask_coeff == 0)
  368.     return data_coeff == 0;
  369.     if (data_coeff == 0 || (mask_coeff > 0) != (data_coeff > 0))
  370.     return false;
  371.     return true;
  372. }
  373. /* Check mask parameters. */
  374. /* Reads ppcv->{matrix,corner,rect}, sets pmcv->{matrix,corner,rect} and */
  375. /* pmcs->{InterleaveType,width,height,full_height,data,y,skip}. */
  376. private bool
  377. check_image3x_mask(const gs_image3x_t *pim, const gs_image3x_mask_t *pimm,
  378.            const image3x_channel_values_t *ppcv,
  379.            image3x_channel_values_t *pmcv,
  380.            image3x_channel_state_t *pmcs, gs_memory_t *mem)
  381. {
  382.     int mask_width = pimm->MaskDict.Width, mask_height = pimm->MaskDict.Height;
  383.     int code;
  384.  
  385.     if (pimm->MaskDict.BitsPerComponent == 0) /* mask missing */
  386.     return 0;
  387.     if (mask_height <= 0)
  388.     return_error(gs_error_rangecheck);
  389.     switch (pimm->InterleaveType) {
  390.     /*case interleave_scan_lines:*/    /* not supported */
  391.     default:
  392.         return_error(gs_error_rangecheck);
  393.     case interleave_chunky:
  394.         if (mask_width != pim->Width ||
  395.         mask_height != pim->Height ||
  396.         pimm->MaskDict.BitsPerComponent != pim->BitsPerComponent ||
  397.         pim->format != gs_image_format_chunky
  398.         )
  399.         return_error(gs_error_rangecheck);
  400.         break;
  401.         if (!is_multiple(mask_height, pim->Height))
  402.         return_error(gs_error_rangecheck);
  403.         /* falls through */
  404.     case interleave_separate_source:
  405.         switch (pimm->MaskDict.BitsPerComponent) {
  406.         case 1: case 2: case 4: case 8:
  407.         break;
  408.         default:
  409.         return_error(gs_error_rangecheck);
  410.         }
  411.     }
  412.     if (!check_image3x_extent(pim->ImageMatrix.xx,
  413.                   pimm->MaskDict.ImageMatrix.xx) ||
  414.     !check_image3x_extent(pim->ImageMatrix.xy,
  415.                   pimm->MaskDict.ImageMatrix.xy) ||
  416.     !check_image3x_extent(pim->ImageMatrix.yx,
  417.                   pimm->MaskDict.ImageMatrix.yx) ||
  418.     !check_image3x_extent(pim->ImageMatrix.yy,
  419.                   pimm->MaskDict.ImageMatrix.yy)
  420.     )
  421.     return_error(gs_error_rangecheck);
  422.     if ((code = gs_matrix_invert(&pimm->MaskDict.ImageMatrix, &pmcv->matrix)) < 0 ||
  423.     (code = gs_point_transform(mask_width, mask_height,
  424.                    &pmcv->matrix, &pmcv->corner)) < 0
  425.     )
  426.     return code;
  427.     if (fabs(ppcv->matrix.tx - pmcv->matrix.tx) >= 0.5 ||
  428.     fabs(ppcv->matrix.ty - pmcv->matrix.ty) >= 0.5 ||
  429.     fabs(ppcv->corner.x - pmcv->corner.x) >= 0.5 ||
  430.     fabs(ppcv->corner.y - pmcv->corner.y) >= 0.5
  431.     )
  432.     return_error(gs_error_rangecheck);
  433.     pmcv->rect.p.x = ppcv->rect.p.x * mask_width / pim->Width;
  434.     pmcv->rect.p.y = ppcv->rect.p.y * mask_height / pim->Height;
  435.     pmcv->rect.q.x = (ppcv->rect.q.x * mask_width + pim->Width - 1) /
  436.     pim->Width;
  437.     pmcv->rect.q.y = (ppcv->rect.q.y * mask_height + pim->Height - 1) /
  438.     pim->Height;
  439.     /* Initialize the channel state in the enumerator. */
  440.     pmcs->InterleaveType = pimm->InterleaveType;
  441.     pmcs->width = pmcv->rect.q.x - pmcv->rect.p.x;
  442.     pmcs->height = pmcv->rect.q.y - pmcv->rect.p.y;
  443.     pmcs->full_height = pimm->MaskDict.Height;
  444.     pmcs->depth = pimm->MaskDict.BitsPerComponent;
  445.     if (pmcs->InterleaveType == interleave_chunky) {
  446.     /* Allocate a buffer for the data. */
  447.     pmcs->data =
  448.         gs_alloc_bytes(mem,
  449.                (pmcs->width * pimm->MaskDict.BitsPerComponent + 7) >> 3,
  450.                "gx_begin_image3x(mask data)");
  451.     if (pmcs->data == 0)
  452.         return_error(gs_error_VMerror);
  453.     }
  454.     pmcs->y = pmcs->skip = 0;
  455.     return 0;
  456. }
  457.  
  458. /*
  459.  * Return > 0 if we want more data from channel 1 now, < 0 if we want more
  460.  * from channel 2 now, 0 if we want both.
  461.  */
  462. private int
  463. channel_next(const image3x_channel_state_t *pics1,
  464.          const image3x_channel_state_t *pics2)
  465. {
  466.     /*
  467.      * The invariant we need to maintain is that we always have at least as
  468.      * much channel N as channel N+1 data, where N = 0 = opacity, 1 = shape,
  469.      * and 2 = pixel.  I.e., for any two consecutive channels c1 and c2, we
  470.      * require c1.y / c1.full_height >= c2.y / c2.full_height, or, to avoid
  471.      * floating point, c1.y * c2.full_height >= c2.y * c1.full_height.  We
  472.      * know this condition is true now; return a value that indicates how to
  473.      * maintain it.
  474.      */
  475.     int h1 = pics1->full_height;
  476.     int h2 = pics2->full_height;
  477.     long current = pics1->y * (long)h2 - pics2->y * (long)h1;
  478.  
  479. #ifdef DEBUG
  480.     if (current < 0)
  481.     lprintf4("channel_next invariant fails: %d/%d < %d/%d\n",
  482.          pics1->y, pics1->full_height,
  483.          pics2->y, pics2->full_height);
  484. #endif
  485.     return ((current -= h1) >= 0 ? -1 :
  486.         current + h2 >= 0 ? 0 : 1);
  487. }
  488.  
  489. /* Define the default implementation of ImageType 3 processing. */
  490. private IMAGE3X_MAKE_MID_PROC(make_midx_default); /* check prototype */
  491. private int
  492. make_midx_default(gx_device **pmidev, gx_device *dev, int width, int height,
  493.          int depth, gs_memory_t *mem)
  494. {
  495.     const gx_device_memory *mdproto = gdev_mem_device_for_bits(depth);
  496.     gx_device_memory *midev;
  497.     int code;
  498.  
  499.     if (mdproto == 0)
  500.     return_error(gs_error_rangecheck);
  501.     midev = gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  502.                 "make_mid_default");
  503.     if (midev == 0)
  504.     return_error(gs_error_VMerror);
  505.     gs_make_mem_device(midev, mdproto, mem, 0, NULL);
  506.     midev->bitmap_memory = mem;
  507.     midev->width = width;
  508.     midev->height = height;
  509.     gx_device_fill_in_procs((gx_device *)midev);
  510.     code = dev_proc(midev, open_device)((gx_device *)midev);
  511.     if (code < 0) {
  512.     gs_free_object(mem, midev, "make_midx_default");
  513.     return code;
  514.     }
  515.     midev->is_open = true;
  516.     dev_proc(midev, fill_rectangle)
  517.     ((gx_device *)midev, 0, 0, width, height, (gx_color_index)0);
  518.     *pmidev = (gx_device *)midev;
  519.     return 0;
  520. }
  521. private IMAGE3X_MAKE_MCDE_PROC(make_mcdex_default);  /* check prototype */
  522. private int
  523. make_mcdex_default(gx_device *dev, const gs_imager_state *pis,
  524.            const gs_matrix *pmat, const gs_image_common_t *pic,
  525.            const gs_int_rect *prect, const gx_drawing_color *pdcolor,
  526.            const gx_clip_path *pcpath, gs_memory_t *mem,
  527.            gx_image_enum_common_t **pinfo,
  528.            gx_device **pmcdev, gx_device *midev[2],
  529.            gx_image_enum_common_t *pminfo[2],
  530.            const gs_int_point origin[2],
  531.            const gs_image3x_t *pim)
  532. {
  533.     /**************** NYI ****************/
  534.     /*
  535.      * There is no soft-mask analogue of make_mcde_default, because
  536.      * soft-mask clipping is a more complicated operation, implemented
  537.      * by the general transparency code.  As a default, we simply ignore
  538.      * the soft mask.  However, we have to create an intermediate device
  539.      * that can be freed at the end and that simply forwards all calls.
  540.      * The most convenient device for this purpose is the bbox device.
  541.      */
  542.     gx_device_bbox *bbdev =
  543.     gs_alloc_struct_immovable(mem, gx_device_bbox, &st_device_bbox,
  544.                   "make_mcdex_default");
  545.     int code;
  546.  
  547.     if (bbdev == 0)
  548.     return_error(gs_error_VMerror);
  549.     gx_device_bbox_init(bbdev, dev);
  550.     gx_device_bbox_fwd_open_close(bbdev, false);
  551.     code = dev_proc(bbdev, begin_typed_image)
  552.     ((gx_device *)bbdev, pis, pmat, pic, prect, pdcolor, pcpath, mem,
  553.      pinfo);
  554.     if (code < 0) {
  555.     gs_free_object(mem, bbdev, "make_mcdex_default");
  556.     return code;
  557.     }
  558.     *pmcdev = (gx_device *)bbdev;
  559.     return 0;
  560. }
  561. private int
  562. gx_begin_image3x(gx_device * dev,
  563.         const gs_imager_state * pis, const gs_matrix * pmat,
  564.         const gs_image_common_t * pic, const gs_int_rect * prect,
  565.         const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
  566.         gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
  567. {
  568.     return gx_begin_image3x_generic(dev, pis, pmat, pic, prect, pdcolor,
  569.                     pcpath, mem, make_midx_default,
  570.                     make_mcdex_default, pinfo);
  571. }
  572.  
  573. /* Process the next piece of an ImageType 3 image. */
  574. private int
  575. gx_image3x_plane_data(gx_image_enum_common_t * info,
  576.              const gx_image_plane_t * planes, int height,
  577.              int *rows_used)
  578. {
  579.     gx_image3x_enum_t *penum = (gx_image3x_enum_t *) info;
  580.     int pixel_height = penum->pixel.height;
  581.     int pixel_used = 0;
  582.     int mask_height[2];
  583.     int mask_used[2];
  584.     int h1 = pixel_height - penum->pixel.y;
  585.     int h;
  586.     const gx_image_plane_t *pixel_planes;
  587.     gx_image_plane_t pixel_plane, mask_plane[2];
  588.     int code = 0;
  589.     int i, pi = 0;
  590.     int num_chunky = 0;
  591.  
  592.     for (i = 0; i < NUM_MASKS; ++i) {
  593.     int mh = mask_height[i] = penum->mask[i].height;
  594.  
  595.     mask_plane[i].data = 0;
  596.     mask_used[i] = 0;
  597.     if (!penum->mask[i].depth)
  598.         continue;
  599.     h1 = min(h1, mh - penum->mask[i].y);
  600.     if (penum->mask[i].InterleaveType == interleave_chunky)
  601.         ++num_chunky;
  602.     }
  603.     h = min(height, h1);
  604.     /* Initialized rows_used in case we get an error. */
  605.     *rows_used = 0;
  606.     if (h <= 0)
  607.     return 0;
  608.  
  609.     /* Handle masks from separate sources. */
  610.     for (i = 0; i < NUM_MASKS; ++i)
  611.     if (penum->mask[i].InterleaveType == interleave_separate_source) {
  612.         /*
  613.          * In order to be able to recover from interruptions, we must
  614.          * limit separate-source processing to 1 scan line at a time.
  615.          */
  616.         if (h > 1)
  617.         h = 1;
  618.         mask_plane[i] = planes[pi++];
  619.     }
  620.     pixel_planes = &planes[pi];
  621.  
  622.     /* Handle chunky masks. */
  623.     if (num_chunky) {
  624.     int bpc = penum->bpc;
  625.     int num_components = penum->num_components;
  626.     int width = penum->pixel.width;
  627.     /* Pull apart the source data and the mask data. */
  628.     /* We do this in the simplest (not fastest) way for now. */
  629.     uint bit_x = bpc * (num_components + num_chunky) * planes[pi].data_x;
  630.     sample_load_declare_setup(sptr, sbit, planes[0].data + (bit_x >> 3),
  631.                   bit_x & 7, bpc);
  632.     sample_store_declare_setup(pptr, pbit, pbbyte,
  633.                    penum->pixel.data, 0, bpc);
  634.     sample_store_declare(dptr[NUM_MASKS], dbit[NUM_MASKS],
  635.                  dbbyte[NUM_MASKS]);
  636.     int depth[NUM_MASKS];
  637.     int x;
  638.  
  639.     if (h > 1) {
  640.         /* Do the operation one row at a time. */
  641.         h = 1;
  642.     }
  643.     for (i = 0; i < NUM_MASKS; ++i)
  644.         if (penum->mask[i].data) {
  645.         depth[i] = penum->mask[i].depth;
  646.         mask_plane[i].data = dptr[i] = penum->mask[i].data;
  647.         mask_plane[i].data_x = 0;
  648.         /* raster doesn't matter */
  649.         sample_store_setup(dbit[i], 0, depth[i]);
  650.         sample_store_preload(dbbyte[i], dptr[i], 0, depth[i]);
  651.         } else
  652.         depth[i] = 0;
  653.     pixel_plane.data = pptr;
  654.     pixel_plane.data_x = 0;
  655.     /* raster doesn't matter */
  656.     pixel_planes = &pixel_plane;
  657.     for (x = 0; x < width; ++x) {
  658.         uint value;
  659.  
  660.         for (i = 0; i < NUM_MASKS; ++i)
  661.         if (depth[i]) {
  662.             sample_load_next12(value, sptr, sbit, bpc);
  663.             sample_store_next12(value, dptr[i], dbit[i], depth[i],
  664.                     dbbyte[i]);
  665.         }
  666.         for (i = 0; i < num_components; ++i) {
  667.         sample_load_next12(value, sptr, sbit, bpc);
  668.         sample_store_next12(value, pptr, pbit, bpc, pbbyte);
  669.         }
  670.     }
  671.     for (i = 0; i < NUM_MASKS; ++i)
  672.         if (penum->mask[i].data)
  673.         sample_store_flush(dptr[i], dbit[i], depth[i], dbbyte[i]);
  674.     sample_store_flush(pptr, pbit, bpc, pbbyte);
  675.     }
  676.     /*
  677.      * Process the mask data first, so it will set up the mask
  678.      * device for clipping the pixel data.
  679.      */
  680.     for (i = 0; i < NUM_MASKS; ++i)
  681.     if (mask_plane[i].data) {
  682.         /*
  683.          * If, on the last call, we processed some mask rows
  684.          * successfully but processing the pixel rows was interrupted,
  685.          * we set rows_used to indicate the number of pixel rows
  686.          * processed (since there is no way to return two rows_used
  687.          * values).  If this happened, some mask rows may get presented
  688.          * again.  We must skip over them rather than processing them
  689.          * again.
  690.          */
  691.         int skip = penum->mask[i].skip;
  692.  
  693.         if (skip >= h) {
  694.         penum->mask[i].skip = skip - (mask_used[i] = h);
  695.         } else {
  696.         int mask_h = h - skip;
  697.  
  698.         mask_plane[i].data += skip * mask_plane[i].raster;
  699.         penum->mask[i].skip = 0;
  700.         code = gx_image_plane_data_rows(penum->mask[i].info,
  701.                         &mask_plane[i],
  702.                         mask_h, &mask_used[i]);
  703.         mask_used[i] += skip;
  704.         }
  705.         *rows_used = mask_used[i];
  706.         penum->mask[i].y += mask_used[i];
  707.         if (code < 0)
  708.         return code;
  709.     }
  710.     if (pixel_planes[0].data) {
  711.     /*
  712.      * If necessary, flush any buffered mask data to the mask clipping
  713.      * device.
  714.      */
  715.     for (i = 0; i < NUM_MASKS; ++i)
  716.         if (penum->mask[i].info)
  717.         gx_image_flush(penum->mask[i].info);
  718.     code = gx_image_plane_data_rows(penum->pixel.info, pixel_planes, h,
  719.                     &pixel_used);
  720.     /*
  721.      * There isn't any way to set rows_used if different amounts of
  722.      * the mask and pixel data were used.  Fake it.
  723.      */
  724.     *rows_used = pixel_used;
  725.     /*
  726.      * Don't return code yet: we must account for the fact that
  727.      * some mask data may have been processed.
  728.      */
  729.     penum->pixel.y += pixel_used;
  730.     if (code < 0) {
  731.         /*
  732.          * We must prevent the mask data from being processed again.
  733.          * We rely on the fact that h > 1 is only possible if the
  734.          * mask and pixel data have the same Y scaling.
  735.          */
  736.         for (i = 0; i < NUM_MASKS; ++i)
  737.         if (mask_used[i] > pixel_used) {
  738.             int skip = mask_used[i] - pixel_used;
  739.  
  740.             penum->mask[i].skip = skip;
  741.             penum->mask[i].y -= skip;
  742.             mask_used[i] = pixel_used;
  743.         }
  744.     }
  745.     }
  746.     if_debug7('b', "[b]image3x h=%d %sopacity.y=%d %sopacity.y=%d %spixel.y=%d\n",
  747.           h, (mask_plane[0].data ? "+" : ""), penum->mask[0].y,
  748.           (mask_plane[1].data ? "+" : ""), penum->mask[1].y,
  749.           (pixel_planes[0].data ? "+" : ""), penum->pixel.y);
  750.     if (penum->mask[0].y >= penum->mask[0].height &&
  751.     penum->mask[1].y >= penum->mask[1].height &&
  752.     penum->pixel.y >= penum->pixel.height)
  753.     return 1;
  754.     /*
  755.      * The mask may be complete (gx_image_plane_data_rows returned 1),
  756.      * but there may still be pixel rows to go, so don't return 1 here.
  757.      */
  758.     return (code < 0 ? code : 0);
  759. }
  760.  
  761. /* Flush buffered data. */
  762. private int
  763. gx_image3x_flush(gx_image_enum_common_t * info)
  764. {
  765.     gx_image3x_enum_t * const penum = (gx_image3x_enum_t *) info;
  766.     int code = gx_image_flush(penum->mask[0].info);
  767.  
  768.     if (code >= 0)
  769.     code = gx_image_flush(penum->mask[1].info);
  770.     if (code >= 0)
  771.     code = gx_image_flush(penum->pixel.info);
  772.     return code;
  773. }
  774.  
  775. /* Determine which data planes are wanted. */
  776. private bool
  777. gx_image3x_planes_wanted(const gx_image_enum_common_t * info, byte *wanted)
  778. {
  779.     const gx_image3x_enum_t * const penum = (const gx_image3x_enum_t *) info;
  780.     /*
  781.      * We always want at least as much of the mask(s) to be filled as the
  782.      * pixel data.
  783.      */
  784.     bool
  785.     sso = penum->mask[0].InterleaveType == interleave_separate_source,
  786.     sss = penum->mask[1].InterleaveType == interleave_separate_source;
  787.  
  788.     if (sso & sss) {
  789.     /* Both masks have separate sources. */
  790.     int mask_next = channel_next(&penum->mask[1], &penum->pixel);
  791.  
  792.     memset(wanted + 2, (mask_next <= 0 ? 0xff : 0), info->num_planes - 2);
  793.     wanted[1] = (mask_next >= 0 ? 0xff : 0);
  794.     if (wanted[1]) {
  795.         mask_next = channel_next(&penum->mask[0], &penum->mask[1]);
  796.         wanted[0] = mask_next >= 0;
  797.     } else
  798.         wanted[0] = 0;
  799.     return false;        /* see below */
  800.     } else if (sso | sss) {
  801.     /* Only one separate source. */
  802.     const image3x_channel_state_t *pics =
  803.         (sso ? &penum->mask[0] : &penum->mask[1]);
  804.     int mask_next = channel_next(pics, &penum->pixel);
  805.  
  806.     wanted[0] = (mask_next >= 0 ? 0xff : 0);
  807.     memset(wanted + 1, (mask_next <= 0 ? 0xff : 0), info->num_planes - 1);
  808.     /*
  809.      * In principle, wanted will always be true for both mask and pixel
  810.      * data if the full_heights are equal.  Unfortunately, even in this
  811.      * case, processing may be interrupted after a mask row has been
  812.      * passed to the underlying image processor but before the data row
  813.      * has been passed, in which case pixel data will be 'wanted', but
  814.      * not mask data, for the next call.  Therefore, we must return
  815.      * false.
  816.      */
  817.     return false
  818.         /*(next == 0 &&
  819.           pics->full_height == penum->pixel.full_height)*/;
  820.     } else {
  821.     /* Everything is chunky, only 1 plane. */
  822.     wanted[0] = 0xff;
  823.     return true;
  824.     }
  825. }
  826.  
  827. /* Clean up after processing an ImageType 3x image. */
  828. private int
  829. gx_image3x_end_image(gx_image_enum_common_t * info, bool draw_last)
  830. {
  831.     gx_image3x_enum_t *penum = (gx_image3x_enum_t *) info;
  832.     gs_memory_t *mem = penum->memory;
  833.     gx_device *mdev0 = penum->mask[0].mdev;
  834.     int ocode =
  835.     (penum->mask[0].info ? gx_image_end(penum->mask[0].info, draw_last) :
  836.      0);
  837.     gx_device *mdev1 = penum->mask[1].mdev;
  838.     int scode =
  839.     (penum->mask[1].info ? gx_image_end(penum->mask[1].info, draw_last) :
  840.      0);
  841.     gx_device *pcdev = penum->pcdev;
  842.     int pcode = gx_image_end(penum->pixel.info, draw_last);
  843.  
  844.     gs_closedevice(pcdev);
  845.     if (mdev0)
  846.     gs_closedevice(mdev0);
  847.     if (mdev1)
  848.     gs_closedevice(mdev1);
  849.     gs_free_object(mem, penum->mask[0].data,
  850.            "gx_image3x_end_image(mask[0].data)");
  851.     gs_free_object(mem, penum->mask[1].data,
  852.            "gx_image3x_end_image(mask[1].data)");
  853.     gs_free_object(mem, penum->pixel.data,
  854.            "gx_image3x_end_image(pixel.data)");
  855.     gs_free_object(mem, pcdev, "gx_image3x_end_image(pcdev)");
  856.     gs_free_object(mem, mdev0, "gx_image3x_end_image(mask[0].mdev)");
  857.     gs_free_object(mem, mdev1, "gx_image3x_end_image(mask[1].mdev)");
  858.     gs_free_object(mem, penum, "gx_image3x_end_image");
  859.     return (pcode < 0 ? pcode : scode < 0 ? scode : ocode);
  860. }
  861.